package com.quad.innovators.salesease.service.impl.warehouse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quad.innovators.salesease.common.utils.SystemCodeUtils;
import com.quad.innovators.salesease.converter.ReturnOrderDetailConverter;
import com.quad.innovators.salesease.converter.ReturnOrderScanConverter;
import com.quad.innovators.salesease.enums.SystemCodeEnum;
import com.quad.innovators.salesease.mapper.warehouse.ReturnOrderDetailMapper;
import com.quad.innovators.salesease.model.entity.warehouse.ReturnOrderDetail;
import com.quad.innovators.salesease.model.entity.warehouse.ReturnOrderScan;
import com.quad.innovators.salesease.model.entity.warehouse.SaleOrderDetail;
import com.quad.innovators.salesease.model.form.warehouse.ReturnOrderDetailForm;
import com.quad.innovators.salesease.model.form.warehouse.SaleOrderDetailForm;
import com.quad.innovators.salesease.model.vo.warehouse.SaleReturnScanNumberVO;
import com.quad.innovators.salesease.model.vo.warehouse.ScanCodeVO;
import com.quad.innovators.salesease.service.warehouse.ReturnOrderDetailService;
import com.quad.innovators.salesease.service.warehouse.ReturnOrderScanService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;

/**
 * @author : shanshan.li
 * @createTime : 2024/12/25 11:38
 * @Version : 1.0 
 */
@Service
@RequiredArgsConstructor
public class ReturnOrderDetailServiceImpl extends ServiceImpl<ReturnOrderDetailMapper, ReturnOrderDetail> implements ReturnOrderDetailService {

    private final ReturnOrderDetailConverter returnOrderDetailConverter;

    private final ReturnOrderScanService returnOrderScanService;

    @Override
    public List<ReturnOrderDetailForm> getListBySaleReturnCode(String code) {
        List<ReturnOrderDetail> returnOrderDetailList = this.baseMapper.selectList(new QueryWrapper<ReturnOrderDetail>().lambda().eq(ReturnOrderDetail::getSaleReturnCode, code));
        List<String> stringList = returnOrderDetailList.stream().map(ReturnOrderDetail::getCode).toList();
        if (CollectionUtils.isNotEmpty(stringList)) {
            List<SaleReturnScanNumberVO> scanNumberByDetailCodeList = returnOrderScanService.getReturnNumberInDetailCodeList(stringList);
            Map<String, Integer> stringIntegerMap = scanNumberByDetailCodeList.stream().collect(Collectors.toMap(SaleReturnScanNumberVO::getSaleReturnOrderDetailCode, SaleReturnScanNumberVO::getSaleReturnOrderNumber));
            return returnOrderDetailList.stream().map(model -> {
                ReturnOrderDetailForm returnOrderDetailForm = returnOrderDetailConverter.toForm(model);
                returnOrderDetailForm.setReturnProductNumberFlag(stringIntegerMap.getOrDefault(model.getCode(), 0).equals(model.getProductNumber()));
                return returnOrderDetailForm;
            }).collect(Collectors.toList());
        }
        return returnOrderDetailList.stream().map(returnOrderDetailConverter::toForm).toList();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveList(List<ReturnOrderDetailForm> returnOrderDetailFormList, String code) {
        // 销售和退货成功一个即可
        if(CollectionUtils.isEmpty(returnOrderDetailFormList)) {
            return true;
        }
        return this.saveBatch(returnOrderDetailFormList.stream().map(model -> {
            ReturnOrderDetail entity = returnOrderDetailConverter.toEntity(model);
            entity.setCode(SystemCodeUtils.getCode(SystemCodeEnum.RETURN_ORDER_DETAIL));
            entity.setSaleReturnCode(code);
            return entity;
        }).toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateList(String code, List<ReturnOrderDetailForm> returnOrderDetailFormList) {
        boolean remove = this.remove(new QueryWrapper<ReturnOrderDetail>().lambda().eq(ReturnOrderDetail::getSaleReturnCode, code));
        return remove && saveList(returnOrderDetailFormList, code);
    }

    @Override
    public List<ScanCodeVO> getScanDetail(String returnOrderDetailCode) {
        return returnOrderScanService.getScanDetail(returnOrderDetailCode);
    }
}